#include "atunemonitorratioframe.h"
#include "ui_atunemonitorratioframe.h"
#include<QDebug>
#include<QMessageBox>

ATuneMonitorRatioFrame::ATuneMonitorRatioFrame(QWidget *parent,Experiment* pExperiment) :
    QFrame(parent),
    ui(new Ui::ATuneMonitorRatioFrame)
{
    ui->setupUi(this);
    this->pExperiment=pExperiment;

    LoadData();
}

ATuneMonitorRatioFrame::~ATuneMonitorRatioFrame()
{
    delete ui;
}

void ATuneMonitorRatioFrame::LoadData()
{
    if(pExperiment==NULL) return;
    AnalyteDataBase *pAnalyteDataBase=pExperiment->GetAnalyteDataBase();

    QStringList elements=pAnalyteDataBase->GetAnalyteElements();
    ui->comboBox_Denominator->clear();
    ui->comboBox_Numerator->clear();
    ui->comboBox_Denominator->addItems(elements);
    ui->comboBox_Numerator->addItems(elements);

    IsotopePara *pIsotopePara=pExperiment->GetIsotopeParaPtr();
    int count=pIsotopePara->GetIsotopePara().count();
    ui->tableWidget->setRowCount(count);
    for(int row=0;row<count;row++)
    {
        int column=0;
        QPair<QString,QString> pair=pIsotopePara->GetIsotopePara().at(row);
        QTableWidgetItem *pItem;
        pItem=new QTableWidgetItem;
        pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
        pItem->setText(pair.first);
        ui->tableWidget->setItem(row,column++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
        pItem->setText(pair.second);
        ui->tableWidget->setItem(row,column++,pItem);
    }
}

void ATuneMonitorRatioFrame::on_pushButton_Add_clicked()
{
    QString name1=ui->comboBox_Numerator->currentText();
    QString name2=ui->comboBox_Denominator->currentText();
    qDebug()<<"name1"<<name1<<"name2"<<name2;
    if(name1=="") return;
    if(name2=="") return;
    if(name1==name2) return;
    QPair<QString,QString> pair;
    pair.first=name1;
    pair.second=name2;
    IsotopePara *pIsotopePara=pExperiment->GetIsotopeParaPtr();
    if(pIsotopePara->AddIsotope(pair)==false) return;

    LoadData();
}

void ATuneMonitorRatioFrame::on_pushButton_Del_clicked()
{
    QModelIndex index=ui->tableWidget->currentIndex();
    if(index.isValid()==false) return;

    QPair<QString,QString> pair;
    pair.first=ui->tableWidget->item(index.row(),0)->text();
    pair.second=ui->tableWidget->item(index.row(),1)->text();
    IsotopePara *pIsotopePara=pExperiment->GetIsotopeParaPtr();
    pIsotopePara->DeleteIsotope(pair);

    LoadData();
}
